<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <script>
        //   10. 九九乘法表
        for (let i = 1; i < 10; i++) {
            let star = ' ';
            for (let num = 1; num < i + 1; num++) {
                star = star + (i + '*' + num + '=' + i * num + ' ')
            }
            console.log(star);
        }
        // 11. while 循环完成 1-100 的求和
        var num = 0;
        var sty = 0;
        while (num < 100) {
            num++;
            sty = sty + num;
            console.log(num);
        }
        console.log(sty);
        // 12. do-while 循环完成 1-100 的求和
        var num = 0;
        var sty = 0;
        do {
            sty = sty + num;
            num++;
        }
        while (num < 101);
        console.log(sty);
        /*    13. 函数完成下面习题：
          [2, 6, 6, 1, 77, 52, 25, 7 ,7]
          1. 数组的最大值
          2. 数组的求和，平均数
          3. 筛选数组中大于 10 的元素放入新的数组
          4. 删除数组中的奇数
          5. 翻转元素，放入新的数组
          6. 数组的去重,形成新的数组
          7. 数组转换为字符串
          8. 数组的排序 */
        // 1. 数组的最大值
        var arr = [2, 6, 6, 1, 77, 52, 25, 7, 7]
        var max = arr[0];
        for (let i = 0; i < arr.length; i++) {
            console.log(arr[i]);
            if (arr[i] > max) {
                max = arr[i];
            }
        }
        console.log("数组的最大值是" + max);
        //  2. 数组的求和，平均数
        var sum = 0;
        for (let i = 0; i < arr.length; i++) {
            sum = sum + arr[i];
        }
        console.log('数组的和是' + sum);
        console.log('数组的平均数是' + sum / arr.length);
        // 3. 筛选数组中大于 10 的元素放入新的数组
        var arr1 = [];
        index = 0;
        for (let item of arr) {
            if (item >= 10) {
                arr1[index] = item;
                index++;
            }
        }
        console.log(arr1);
        // 4. 删除数组中的奇数
        var arr = [2, 6, 6, 1, 77, 52, 25, 7, 7]
        var arr1 = [];
        for (let i = 0; i < arr.length; i++) {
            if (arr[i] % 2 == 0) {
                arr1[arr1.length] = arr[i];
            }
        }
        console.log(arr1);
        // 5. 翻转元素，放入新的数组
        var arr1 = [];
        for (let i = arr.length - 1; i >= 0; i--) {
            arr1[arr1.length] = arr[i];
        }
        console.log(arr1);
        // 6. 数组的去重,形成新的数组
        var arr =   [2, 6, 6, 1, 77, 52, 25, 7 ,7];
        var arr1 = [];

        for (let i = 0; i < arr.length; i++) {
            console.log(arr[i]);
            let flag = false;
            for (let item of arr1) {
                if (arr[i] == item) {
                    console.log('该元素已存在');
                    flag = true;
                }
            }
            if (!flag) {
                arr1[arr1.length] = arr[i];
            }
        }
        console.log(arr1);
        // 7. 数组转换为字符串

        var result = "[";
        for (let index in arr) {
            if (index == arr.length - 1) {
                result = result + "\'" + arr[index] + "\'";
            } else {
                result = result + "\'" + arr[index] + "\'" + ", ";
            }
        }
        result = result + "]";
        console.log(result);
        // 8. 数组的排序 
        
       
        function sort(arr) {
            for (let j = 0; j < arr.length - 1; j++) {
                for (let i = 0; i < arr.length - 1 - j; i++) {
                    if (arr[i] > arr[i + 1]) {
                        let temp = arr[i];
                        arr[i] = arr[i + 1];
                        arr[i + 1] = temp;
                    }
                }
            }
        }
        sort(arr1);

        console.log(arr1);


    </script>
</body>

</html>